///<reference path="../typings/jquery/jquery.d.ts">
///<reference path="../typings/sharepoint/sharepoint.d.ts">
///<reference path="utilities.ts">
define(["require", "exports", "modules/utilities"], function(require, exports, __m_Util__) {
    var m_Util = __m_Util__;

    (function (Taxonomy) {
        var Dependencies = (function () {
            function Dependencies() {
            }
            Dependencies.GetTermStore = function (termStoreName) {
                var termStore = new m_Util.Utilities.Dependency();

                // Overwrite delegate
                termStore.Resolve = function () {
                    // Current Context
                    var context = SP.ClientContext.get_current();

                    // Current Taxonomy Session
                    var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);

                    // Term Stores
                    var termStores = taxSession.get_termStores();

                    context.load(termStores);

                    context.executeQueryAsync(function () {
                        var termStoreEnumerator = termStores.getEnumerator();
                        while (termStoreEnumerator.moveNext()) {
                            if (termStoreEnumerator.get_current().get_name() == termStoreName) {
                                // Update the Observable
                                termStore.AsyncObservable(termStoreEnumerator.get_current());
                                alert(termStoreEnumerator.get_current().get_name());
                            }
                        }
                    }, function () {
                        alert("ExecuteQueryAsyncException@termStore.Resolve");
                    });
                };
                return termStore;
            };

            Dependencies.GetTermGroup = function (termStore, termGroupName) {
                var termGroup = new m_Util.Utilities.Dependency();

                // Overwrite delegate
                termGroup.Resolve = function () {
                    // Current Context
                    var context = SP.ClientContext.get_current();

                    // Current Taxonomy Session
                    var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context);

                    // Term Groups
                    var termGroups = termStore.AsyncObservable().get_groups();

                    context.load(termGroups);

                    context.executeQueryAsync(function () {
                        var termGroupsEnumerator = termGroups.getEnumerator();
                        while (termGroupsEnumerator.moveNext()) {
                            if (termGroupsEnumerator.get_current().get_name() == termGroupName) {
                                termGroup.AsyncObservable(termGroupsEnumerator.get_current());
                                alert(termGroup.AsyncObservable().get_name());
                            }
                        }
                    }, function () {
                        alert("ExecuteQueryAsyncException@termGroup.Resolve");
                    });
                };
                return termGroup;
            };

            Dependencies.GetTermSet = function (termGroup, termSetName) {
                var termSet = new m_Util.Utilities.Dependency();

                // Overwrite delegate
                termSet.Resolve = function () {
                    // Current Context
                    var context = SP.ClientContext.get_current();

                    // Terms for termLabel
                    var termSets = termGroup.AsyncObservable().get_termSets();

                    context.load(termSets);

                    context.executeQueryAsync(function () {
                        var termSetEnumerator = termSets.getEnumerator();
                        while (termSetEnumerator.moveNext()) {
                            if (termSetEnumerator.get_current().get_name() == termSetName) {
                                termSet.AsyncObservable(termSetEnumerator.get_current());
                                alert(termSet.AsyncObservable().get_name());
                            }
                        }
                    }, function () {
                        alert("ExecuteQueryAsyncException@termSet.Resolve");
                    });
                };
                return termSet;
            };
            return Dependencies;
        })();
        Taxonomy.Dependencies = Dependencies;
    })(exports.Taxonomy || (exports.Taxonomy = {}));
    var Taxonomy = exports.Taxonomy;
});
//# sourceMappingURL=Taxonomy.js.map
